﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading.Tasks;

namespace ExcelAddIn1
{
    /**
     * This class holds one dataset (column) read from en Excel spreadsheet.
     */
    class DataSet
    {
        //Dataset label
        private String name;
        //List of data points.
        private List<double> values = new List<double>();
        private double stdev = 0.0;
        private double mean = 0.0;

        /**
         * Creates a new dataset with a specific label.
         */
        public DataSet(String name)
        {
            this.name = name;
        }

        /**
         * Creates a new dataset with a default label.
         */
        public DataSet()
        {
            this.name = "Serie";
        }

        /**
         * Returns the name (label) of the dataset.
         */
        public String GetName()
        {
            return name;
        }

        /**
         * Adds a datapoint to this dataset.
         */
        public void AddValue(double value)
        {
            values.Add(value);
        }

        /**
         * Returns a specific datapoint in this dataset.
         */
        public double GetValue(int index)
        {
            return values[index];
        }

        /**
         * Returns the sample size, n, of this dataset.
         */
        public int GetN()
        {
            return values.Count;
        }

        /**
         * Returns the mean value of this dataset.
         */
        public double GetMean()
        {
            if (mean == 0.0)
            {
                double sum = 0.0;
                foreach (double v in values)
                {
                    sum += v;
                }
                mean = sum / (double)GetN();
            }
            return mean;
        }

        /**
         * Returns the sample standard deviation of this dataset. 
         */
        public double GetStDev()
        {
            //Calculate standard deviation of the data set
            //using the standard method
            if (stdev == 0.0)
            {
                double sumDiff2 = 0.0;
                double mean = GetMean();
                double n = (double)(GetN() - 1);

                foreach (double v in values)
                {
                    sumDiff2 += Math.Pow(v - mean, 2);
                }

                double variance = sumDiff2 / n;
                stdev = Math.Sqrt(variance);
            }
            return stdev;
        }

        /**
         * Returns the sum of all datapoints in this dataset.
         */
        public double GetSum()
        {
            double sum = 0.0;
            foreach (double v in values)
            {
                sum += v;
            }
            return sum;
        }

        /**
         * Returns the squared sum of all datapoints in this dataset.
         */
        public double GetSquaredSum()
        {
            double sum = 0.0;
            foreach (double v in values)
            {
                sum += Math.Pow(v, 2);
            }
            return sum;
        }
    }
}
